package com.framework.config.advice;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionControllerAdvice {

	private static final Logger log = LoggerFactory.getLogger(ExceptionControllerAdvice.class);

	@ExceptionHandler(Exception.class)
	public ModelAndView handleException(Exception ex) {
		ModelAndView mav = new ModelAndView("/resources/html/errorPage/error500.html");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "E0000");
		map.put("message", ex.getMessage());
		log.warn("Exception : {}", ex);
		mav.addObject(map);
		return mav;
	}

	@ExceptionHandler(RuntimeException.class)
	public ModelAndView handleRuntimeException(Exception ex) {
		ModelAndView mav = new ModelAndView("/resources/html/errorPage/error500.html");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "E0001");
		map.put("message", ex.getMessage());
		log.warn("Exception : {}", ex);
		mav.addObject(map);
		return mav;
	}

	@ExceptionHandler(SQLException.class)
	public ModelAndView handleSQLException(Exception ex) {
		ModelAndView mav = new ModelAndView("/resources/html/errorPage/error500.html");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "E0002");
		map.put("message", ex.getMessage());
		log.warn("Exception : {}", ex);
		mav.addObject(map);
		return mav;
	}

	@ExceptionHandler(IOException.class)
	public ModelAndView handleIOException(Exception ex) {
		ModelAndView mav = new ModelAndView("/resources/html/errorPage/error500.html");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", "E0003");
		map.put("message", ex.getMessage());
		log.warn("Exception : {}", ex);
		mav.addObject(map);
		return mav;
	}
}
